package policy

type tBubbleSortPolicy struct {
}

func NewBubbleSortPolicy() ISortPolicy {
	return &tBubbleSortPolicy{}
}

func (self *tBubbleSortPolicy) Name() string {
	return "BubbleSort"
}

func (self *tBubbleSortPolicy) Sort(data []int) []int {
	if data == nil {
		return nil
	}

	size := len(data)
	if size <= 1 {
		return data
	}

	for {
		i := size - 1
		changed := false

		for {
			if i <= 0 {
				break
			}
			j := i - 1

			if data[j] > data[i] {
				data[i],data[j] = data[j],data[i]
				changed = true
			}

			i--
		}

		if !changed {
			break
		}
	}

	return data
}
